import { Module } from 'vuex';
import { getResource } from '@/api/resource';
import { getPrograms, getEqptTypes,getcompanys } from '@/api/requst/management';


export interface Repairstate {
    // 航站楼
    terminal: any[],
    // 问题类型
    programs: any[],
    // 设备类型
    eqptTypes: any[],
    //外部单位
    companys: [],
    //问题归属
    ascription:string,
    //地图返回的经纬度
    coordinate: {}
}

const repair: Module<Repairstate, any> = {
    namespaced: true,
    state: () => ({
        terminal: [],
        programs: [],
        eqptTypes: [],
        companys: [],
        ascription:'',
        coordinate: {}
    }),
    getters: {
        getTermianl(state) {
            return state.terminal
        },
        getPrograms(state) {
            return state.programs.map((item: any) => {
                return {
                    label: item.problemType,
                    value: item.problemId
                }
            })
        },
        getEqptTypes(state) {
            const handleTree = (data: any) => {
                data.forEach((item: any) => {
                    item.label = item.typeName
                    item.value = item.typeId
                    if (item.children) {
                        handleTree(item.children)
                    }
                })
            }
            handleTree(state.eqptTypes)
            return state.eqptTypes
        },
        getCoordinate(state) {
            return state.coordinate
        },
        getCompanyse(state) {
            return state.companys
        },
        //问题类型
        getascription(state){
            return state.ascription
        }
    },
    mutations: {
        SET_TERMIANL(state: any, payload: any) {
            state.terminal = payload
        },
        SET_PROGRAMS(state: any, payload: any) {
            state.programs = payload
        },
        SET_EQPT_TYPES(state: any, payload: any) {
            state.eqptTypes = payload
        },
        SET_COOR_DINATE(state:any,payload:any){
            state.coordinate = payload
        },
        SET_COMPANYSE(state:any,payload:any){
            state.companys = payload
        },
        SET_ascription(state:any,payload:any){
            state.ascription=payload
        }
    },
    actions: {
        async setTerminal({ commit }) {
            const res = await getResource('terminal')
            if (res.data) {
                const { properties } = res.data
                commit('SET_TERMIANL', properties.meta || [])
            }
        },
        async setPrograms({ commit }) {
            const res = await getPrograms({})
            if (res.data) {
                commit('SET_PROGRAMS', res.data || [])
            }
        },
        async setEqptTypes({ commit }) {
            const res = await getEqptTypes({})
            if (res.data) {
                commit('SET_EQPT_TYPES', res.data || [])
            }
        },
        async setCompanys({commit}){
            const res=await getcompanys()            
                commit('SET_COMPANYSE', res.data || [])
        }
    }
}

export default repair